package com.example.backend.filter;

import com.example.backend.mapper.FunctionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Jcy
 * @Date 2022/5/13
 */
@Component
public class BookletFilter implements HandlerInterceptor {
    @Autowired
    private FunctionMapper functionMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(functionMapper.selectById(3).getFunction_status()==0){
            response.setContentType("application/json;charset=UTF-8");
            //设置编码格式
            response.setCharacterEncoding("UTF-8");
            response.setStatus(400);
            response.getWriter().write("{\n" +
                    "\t\"resultCode\": 400,\n" +
                    "\t\"message\": \"功能已锁定，请联系管理员解锁\",\n" +
                    "\t\"data\": null\n" +
                    "}");
            return false;
        }

        return true;
    }
}